$(function() {
    var action = '';
    const radiu = 30; //计数时间
    // 判断是否抓取到，是否消除
    var goldNo = new Array(4);
    var goldPosition = new Array(4);
    goldInit();
    goldPos();
    var position = -1;
    console.log(goldPosition);
    var isMove = false;
    var length = {
        now: 0,
        limitMin: 100,
        limitMax: 800,
        direction: 0,
    }
    var angle = 50;
    const angleLimit = {
        left: 20,
        right: 160,
        direction: 0
    };
    // ----------------------------------------测试类--------------------------------------------

    console.log("main.js文件引入成功");
    catchRotato();

    function catchRotato() {
        setInterval(function() {
            if (!isMove) {
                angleChange();
            } else {
                lengthChange();
            }
            isCatch();
        }, radiu);

        // 角度旋转
        function angleChange() {
            var item = $('#catch');
            if (angleLimit.direction == 0) {
                if (angle < angleLimit.right) {
                    angle++;
                } else {
                    angleLimit.direction = 1;
                }
            } else {
                if (angle > angleLimit.left) {
                    angle--;
                } else {
                    angleLimit.direction = 0;
                }
            }
            var rotate = 'rotate(' + String(angle) + 'deg)';
            // console.log(rotate);
            item.css('transform', rotate);
        }

        // 抓钩伸长
        function lengthChange() {
            var item = $('.end');
            // 测试修改长度
            // item.css('margin-left', '300px');
            if (length.now < length.limitMin) {
                length.now = length.limitMin;
            }
            if (length.direction == 0) {
                if (length.now < length.limitMax) {
                    length.now += 10;
                } else {
                    length.direction = 1;
                }
            } else {
                if (length.now > length.limitMin) {
                    length.now -= 10;
                } else {
                    length.direction = 0;
                    isMove = false;
                }
            }
            var cssLength = String(length.now) + 'px';
            item.css('margin-left', cssLength);
        }
    }

    function isCatch() {
        var item = $('.end');
        var x = item.offset().left;
        var y = item.offset().top;;
        // console.log(x + "/ " + y);
        inReact();
        // console.log(goldNo);

        function inReact() {
            if (isMove) {
                if (position == -1) {
                    for (var i = 0; i < 4; i++) {
                        if (goldPosition[i][0] < x && x < goldPosition[i][0] + 100 &&
                            goldPosition[i][1] < y && y < goldPosition[i][1] + 100 && goldNo[i] != 0
                        ) {
                            console.log(i + "// ");
                            length.direction = 1;
                            position = i;
                            // break;
                        }
                    }
                } else {
                    goldPosition[position][0] = x;
                    goldPosition[position][1] = y;
                    goldPos();
                }

            } else {
                $('.gold div').eq(position).hide();
                goldNo[position] = 0;
                position = -1;
            }


        }
    }

    function goldPos() {
        var items = $('.gold div');
        for (var i = 0; i < 4; i++) {
            if (goldNo[i] != 0) {
                items.eq(i).offset({
                    top: goldPosition[i][1],
                    left: goldPosition[i][0]
                })
            }

            // items.eq(i).css('background', 'red');
        }
    }

    function goldInit() {
        goldPosition = [
            [200, 400],
            [199, 599],
            [499, 329],
            [622, 590]
        ];
        goldNo = [1, 1, 1, 1];
        // console.log(goldPosition);
    }










    // ---------------------------------键盘监听---------------------------------------
    $('body').keydown(function(e) {
        switch (e.keyCode) {
            case 40:
                console.log("down");
                action = "down";
                isMove = true;
                // console.log(isMove);
                break;
        }
    });
})